// pages/myTeam/myTeam.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectArray: [{
      "id": "30",
      "text": "全部"
    },{
      "id": "10",
      "text": "男"
    }, {
      "id": "21",
      "text": "女"
    }],
    array: ["全部","01 ~ 10", "11 ~ 20","21 ~ 30", "31 ~ 40","41 ~ 50", "51 ~ 60","61 ~ 70", "71 ~ 80","81 ~ 90","91 ~ 100"],
    index:0,
    downImg:'/resources/down.png',
    upImg: '/resources/up.png',
    healthBtn: true,
    sugarBtn: true,
    pressureBtn: true,
  },

  //picker
  bindAgeChange: function (e) {
    this.setData({
      index: e.detail.value,
      interval: this.data.array[e.detail.value],
      ageImgsrc: this.data.downImg,
    })
    this.selectAgeDataList(this.data.interval)
  },
  
  bindAgeCancel:function(){
    this.setData({
      ageImgsrc: this.data.downImg
    })
  },

  //性别选项获得数据
  getDate:function(e){
    // console.log(e.details)
    this.setData({
      sex: e.detail
    })
    this.selectSexDataList(e.detail)
  },

  //选择箭头
  imgChange:function(e)
  {
    var that = this
    if(that.data.ageImgsrc == that.data.downImg)
    {
      that.setData({
        ageImgsrc: that.data.upImg
      })
    }
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.setData({
      ageImgsrc:this.data.downImg
    })
    that.getTeamData();
    if(options.stateList){
      that.setData({
        stateList : JSON.parse(options.stateList)
      })
      that.isShowBtn(JSON.parse(options.stateList))
    }
  },

  //是否显示按钮
  isShowBtn:function(stateList){
    var that = this
    for(var i in stateList){
      if(stateList[i].type == 1 && stateList[i].status == 2){
        that.setData({
          healthBtn: false
        })
      }
      if(stateList[i].type == 2 && stateList[i].status == 2){
        that.setData({
          sugarBtn: false
        })
      }
      if(stateList[i].type == 3 && stateList[i].status == 2){
        that.setData({
          pressureBtn: false
        })
      }
    }
  },

  //获取数据
  getTeamData:function(){
    var that = this
    wx.request({
      url: app.globalData.apiUrl +'/team',
      data: {
        userId: app.globalData.userId
      },
      method: 'get',
      header: {
        'content-type': 'application/json' //默认值
      },
      success: function (res) {
        // console.log(res)
        if(res.statusCode == '200'){
          that.setData({
            teamList:res.data,
            allList: res.data
          })
        }
      },
      fail: function (res) {
        console.log("失败");
      }
    })
  },

  //根据性别选择 显示数据列表
  selectSexDataList:function(sex){
    var that = this
    var all = that.data.allList;
    var arr = [];
    for(var i in all){
      if(!that.data.interval){
        if(sex.text == "全部")
        {
          arr.push(all[i])
        }else{
          if(all[i].sex == sex.text)
          {
            arr.push(all[i])
          }
        }
      }else
      {
        if(sex.text == "全部"){
          var age =  that.ageInterval(all[i],that.data.interval)
          if(age!=false)
          {
            arr.push(all[i])
          }
        }
        else{
          var age =  that.ageInterval(all[i],that.data.interval)
          if(all[i].sex == sex.text && age != false){
            arr.push(all[i])
          }
        }
      }
    }
    that.setData({
      teamList: arr
    })
  },

  //根据年龄区间选择 显示数据列表
  selectAgeDataList:function(interval){
    var that = this
    var all = that.data.allList;
    var arr = [];
    console.log(interval)
    for(var i in all){
      if(!that.data.sex){
        var age =  that.ageInterval(all[i],interval)
        // console.log(age)
        if(age != false)
        {
          arr.push(all[i])
        }
      }else{
        var age =  that.ageInterval(all[i],interval)
        if(all[i].sex == that.data.sex.text && age != false)
        {
          arr.push(all[i])
        }
      }
    }
    that.setData({
      teamList: arr
    })
  },

  //选择年龄区间
  ageInterval:function(i,interval){
    if(interval == '全部')
    {
      return i
    }
    if(interval == '01 ~ 10')
    {
      if(i.age >= 1 && i.age <= 10)
      {
        return i
      }
      else{
        return false
      }
    }
    if(interval == '11 ~ 20')
    {
      if(i.age >= 11 && i.age <= 20)
      {
        console.log(i.age)
        return i
      }
      else{
        return false
      }
    }
    if(interval == '21 ~ 30')
    {
      if(i.age >= 21 && i.age <= 30)
      {
        return i
      }
      else{
        return false
      }
    }
    if(interval == '31 ~ 40')
    {
      if(i.age >= 31 && i.age <= 40)
      {
        return i
      }
      else{
        return false
      }
    }
    if(interval == '41 ~ 50')
    {
      if(i.age >= 41 && i.age <= 50)
      {
        return i
      }
      else{
        return false
      }
    }
    if(interval == '51 ~ 60')
    {
      if(i.age >= 51 && i.age <= 60)
      {
        return i
      }
      else{
        return false
      }
    }
    if(interval == '61 ~ 70')
    {
      if(i.age >= 61 && i.age <= 70)
      {
        return i
      }
      else{
        return false
      }
    }
    if(interval == '71 ~ 80')
    {
      if(i.age >= 71 && i.age <= 80)
      {
        return i
      }
      else{
        return false
      }
    }
    if(interval == '81 ~ 90')
    {
      if(i.age >= 81 && i.age <= 90)
      {
        return i
      }
      else{
        return false
      }
    }
    if(interval == '91 ~ 100')
    {
      if(i.age >= 91 && i.age <= 100)
      {
        return i
      }
      else{
        return false
      }
    }
  },

  //查看按钮
  pBtn:function(e){
    let name = e.currentTarget.dataset.name
    if(name == "体重"){
      wx.navigateTo({
        url: '/subPages/bodyFat/history/history?item='+ JSON.stringify(e.currentTarget.dataset.item),
      })
    }
    if(name == "体趋势"){
      wx.navigateTo({
        url: '/subPages/bodyFat/bodyTrend/bodyTrend?item='+ JSON.stringify(e.currentTarget.dataset.item),
      })
    }
    if(name == "血糖"){
      wx.navigateTo({
        url: '/subPages/glucometer/history/history?item='+ JSON.stringify(e.currentTarget.dataset.item),
      })
    }
    if(name == "血压"){
      wx.navigateTo({
        url: '/subPages/pressure/history/history?item='+ JSON.stringify(e.currentTarget.dataset.item),
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: 'PAPN小程序',
      path: 'pages/index/index?puid=' + app.globalData.userId,
      success: function(res) {
          res.shareTickets 
      },
      fail: function(res) {
          console.log(res)
      }
    }
  }
})